package gmail;

import gmail.structure.GMConversationEntry;

import java.util.ArrayList;
import java.util.TreeMap;

/**
 * Response from GMail that is a conversation
 * Each conversation contains one title, one id, set of labels and entries(message)
 * @author siuying
 * @version 0.3.4
 */

public class GMConversation extends GMResponse implements java.io.Serializable {
  /**
     * 
     */
    private static final long serialVersionUID = 7675801788587471379L;
/**
   * Type of this response
   */
  public byte type = GMConstants.GM_REQ_CONVERSATION;
  private String title;
  private int total;
  private String id;
  private ArrayList labels;
  private ArrayList entries;

  public GMConversation() {
    super();
    entries = new ArrayList();
    labels = new ArrayList();
  }

  public GMConversation(TreeMap gminfo, TreeMap gmlabel, ArrayList threads) {
    super(gminfo, gmlabel, threads);
    entries = new ArrayList();
  }
  public String toString(){
    StringBuffer buf = new StringBuffer();

    buf.append("GMConversation: \n{");
    buf.append("\tgminfo: " + _gminfo + ", \n");
    buf.append("\tgmlabel: " + _gmlabel + ", \n");
    buf.append("\ttitle: " + title + ", \n");
    buf.append("\ttotal: " + total + ", \n");
    buf.append("\tid: " + id + ", \n");
    buf.append("\tentries: " + entries + ", \n");
    buf.append("\tlabels: " + labels + "}");

    return buf.toString();
  }
  
  /**
   * get id of the entries, in String array
   * @return String[] ids of entries in the conversation
   * @since 0.3.3.4
   */
  public String[] getEntriesId(){
      String[] ids = new String[entries.size()];
      for (int i=0; i<entries.size(); i++){
            GMConversationEntry entry = (GMConversationEntry)entries.get(i);
            ids[i] = entry.getId();
      }
      return ids;
  }
  
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  public void setTotal(int total) {
    this.total = total;
  }
  public int getTotal() {
    return total;
  }
  public byte getType() {
    return type;
  }
  public void setType(byte type) {
    this.type = type;
  }
  public void setLabels(ArrayList labels) {
    this.labels = labels;
  }
  public void setLabels(String[] labels) {
    this.labels.clear();
    for(int i=0; i<labels.length; i++){
        this.labels.add(labels[i]);    
    }
  }
  public GMConversationEntry[] getEntriesArray(){    
    return (GMConversationEntry[])entries.toArray(new GMConversationEntry[0]);
  }
  public ArrayList getEntries(){    
    return entries;
  }
  public void setEntries(ArrayList entries){
    this.entries = entries;
  }
  public void addEntries(GMConversationEntry entry){
    this.entries.add(entry);    
  }
  public ArrayList getLabels() {                     
    return labels;
  }
  
  public boolean equals(GMConversationEntry target){
    if (target == null)
    {
      return false;
    }
    else
    {
      return this.getId().equals(target.getId());  
    }
  }
}
